﻿//Restriction Module v 1.0
//Written by Bugra POSTACI (c)
//August 2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace RestrictionModuleSharepoint2010
{
    public class RestrictionModuleFeatureReciever : SPFeatureReceiver
    {
        private readonly string _ModificationOwner = "RestrictionModuleFeatureReciever";
        private readonly string _AppSettingsXPath = "configuration/appSettings";
        private readonly string _ModuleXPath = "configuration/system.web/httpModules"; 
        
        private static string _SiteAppSettingsKey = "RestrictionModuleActive";
        private static string _ModuleName = "RestrictionModule";
        private static string _ModuleType = "RestrictionModuleApp.RestrictionModule, RestrictionModuleApp,Version=1.0.0.0, Culture=neutral, PublicKeyToken=be23a05ec1781ff6";
        private readonly string _SiteUrlModification = string.Format("add[@key=\"{0}\"]", _SiteAppSettingsKey);
        private readonly string _SiteUrlValue = "";

        private readonly string _ModuleModification = string.Format("add[@name=\"{0}\"][@type=\"{1}\"]"
            , _ModuleName, _ModuleType); 

       
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //do nothing 
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            bool bApplyChanges = ApplyWebConfigMods(properties);
            SPSite currentSite = properties.Feature.Parent as SPSite;

            if (bApplyChanges)
            {
                SPWebConfigModification siteEntry = addAppSetting(_SiteUrlModification,
                    _AppSettingsXPath, _SiteAppSettingsKey, "Off", 100);

                SPWebConfigModification moduleEntry = addHttpModule(_ModuleModification, _ModuleXPath, _ModuleName, _ModuleType,0); 


                SPWebApplication currentWebApp = currentSite.WebApplication;
                currentWebApp.WebConfigModifications.Add(siteEntry);
                currentWebApp.WebConfigModifications.Add(moduleEntry); 
                currentWebApp.WebService.ApplyWebConfigModifications();
                currentWebApp.Update();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPSite currentSite = properties.Feature.Parent as SPSite;
            SPWebApplication currentWebApp = currentSite.WebApplication;

            Collection<SPWebConfigModification> colMods = currentWebApp.WebConfigModifications;
            Collection<SPWebConfigModification> colForRemoval = new Collection<SPWebConfigModification>();

            foreach (SPWebConfigModification mod in colMods)
            {
                if (mod.Owner == _ModificationOwner)
                {
                    colForRemoval.Add(mod);
                }
            }

            foreach (SPWebConfigModification modForRemoval in colForRemoval)
            {
                colMods.Remove(modForRemoval);
            }

            if (colForRemoval.Count > 0)
            {
                currentWebApp.WebService.ApplyWebConfigModifications();
                currentWebApp.Update();
            }
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //do nothing
        }

        private SPWebConfigModification addAppSetting(string sModificationName, string sXpath,
         string sKey, string sValue, uint iSequence)
        {
            SPWebConfigModification configMod = new SPWebConfigModification(sModificationName, sXpath);
            configMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            configMod.Name = sModificationName;
            configMod.Owner = _ModificationOwner;

            configMod.Value = string.Format("<add key=\"{0}\" value=\"{1}\" />",
                 sKey, sValue);
            configMod.Sequence = iSequence;

            return configMod;
        }

        private SPWebConfigModification addHttpModule(string sModificationName, string sXpath,
             string moduleName, string type, uint iSequence)
        {
            SPWebConfigModification configMod = new SPWebConfigModification(sModificationName, sXpath);
            configMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            configMod.Name = sModificationName;
            configMod.Owner = _ModificationOwner;

            configMod.Value = string.Format("<add name=\"{0}\" type=\"{1}\" />",
                 moduleName, type);
            configMod.Sequence = iSequence;

            return configMod;
        }

        private bool ApplyWebConfigMods(SPFeatureReceiverProperties properties)
        {
            bool bApplyChanges = true;
            SPFeaturePropertyCollection featureProps = properties.Feature.Properties;
            if (featureProps != null && featureProps.Count > 0)
            {
                SPFeatureProperty applyModsProp = null;
                try
                {
                    applyModsProp = featureProps["ApplyWebConfigModifications"];

                    if (applyModsProp != null)
                    {
                        bool.TryParse(applyModsProp.Value, out bApplyChanges);
                    }
                }
                catch
                {
                    // we'll go ahead and make the web.config changes if the property is not present..
                }
            }
            return bApplyChanges;
        }
    }
}
